﻿<%@ WebHandler Language="C#" Class="ReportPageMaint" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ReportBusiness;

/**
 * Description: MES报表维护
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.10.20
 **/

public class ReportPageMaint : IHttpHandler
{
    public bool IsReusable { get { return false; } }
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    TreeObject t = new TreeObject();
    List<TreeObject> treeNodes = new List<TreeObject>();
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询数据
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "getPageByid":
                        r = getPageByid(jObject);//根据pageid查
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "addPage":
                        r = addPage(jObject);//添加报表
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "updateStatus":
                        r = updateStatus(jObject);//更新启用状态
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "update":
                        r = update(jObject);//更新报表信息
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "deletePage":
                        r = deletePage(jObject);//删除报表
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "getWorkcenter":
                        r = getWorkcenter();//查询工区
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    //查询数据
    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = @"
SELECT t.pageid,t.pagename,t.pageurl,t.createdate,t.modifydate,e.fullname createperson,e2.fullname modifyperson,
       t.pagetype,
       t.status,
       t.factoryid,
       f.factoryname,
       t.workcenterid,
       w.workcentername,
       t.isnew
FROM mes_reportpage t
LEFT JOIN factory f ON f.factoryid = t.factoryid
LEFT JOIN Workcenter w ON w.workcenterid = t.workcenterid
LEFT JOIN employee e on e.employeeid = t.createemployeeid
LEFT JOIN employee e2 on e2.employeeid = t.modifyemployeeid
WHERE 1=1 
";
        if (jObject["pagetype"] != null && jObject["pagetype"].ToString() != "")
        {
            sql += string.Format(" AND t.pagetype = '{0}'", jObject["pagetype"].ToString());
        }

        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND t.factoryid = '{0}'", jObject["factoryid"].ToString());
        }

        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND t.workcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        if (jObject["pagename"] != null && jObject["pagename"].ToString() != "")
        {
            sql += string.Format(" AND t.pagename like '%{0}%'", jObject["pagename"].ToString());
        }

        if (jObject["pageurl"] != null && jObject["pageurl"].ToString() != "")
        {
            sql += string.Format(" AND t.pageurl like '%{0}%'", jObject["pageurl"].ToString());
        }

        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);

        DataTable dt = dto.DBTable;

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //根据id查询
    Result getPageByid(JObject jObject)
    {
        Result r = new Result();
        string sql = @"
SELECT t.pageid,t.pagename,t.pageurl,t.createdate,t.modifydate,e.fullname createperson,e2.fullname modifyperson,
       t.pagetype,
       t.status,
       t.factoryid,
       f.factoryname,
       t.workcenterid,
       w.workcentername,
       t.isnew
FROM mes_reportpage t
LEFT JOIN factory f ON f.factoryid = t.factoryid
LEFT JOIN Workcenter w ON w.workcenterid = t.workcenterid
LEFT JOIN employee e on e.employeeid = t.createemployeeid
LEFT JOIN employee e2 on e2.employeeid = t.modifyemployeeid
WHERE t.pageid='{0}'
";
        sql = string.Format(sql, jObject["pageid"].ToString());

        DataTable dt = dal.GetDataTable(sql);

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        r.data = dt;
        r.total = Convert.ToInt32(dt.Rows.Count);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //增加报表
    Result addPage(JObject jObject)
    {
        Result r = new Result();
        string sql = @"
INSERT INTO mes_reportpage(pageid,pagename,pageurl,pagetype,createemployeeid,factoryid,workcenterid,isnew,status,createdate)
VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','1',sysdate)
";

        string pageid = Guid.NewGuid().ToString();
        string pagename = jObject["pagename"] != null ? jObject["pagename"].ToString() : "";
        string pageurl = jObject["pageurl"] != null ? jObject["pageurl"].ToString() : "";
        string pagetype = jObject["pagetype"].ToString();
        string createemployeeid = jObject["employeeid"].ToString();
        string factoryid = jObject["factoryid"] != null ? jObject["factoryid"].ToString() : "";
        string workcenterid = jObject["workcenterid"] != null ? jObject["workcenterid"].ToString() : "";
        string isnew = "0";
        if (jObject["isnew"] != null && jObject["isnew"].ToString() == "1")
        {
            isnew = "1";
        }
        sql = string.Format(sql, pageid, pagename, pageurl, pagetype, createemployeeid, factoryid, workcenterid, isnew);

        int count = dal.ExecuteSql(sql);

        r.data = null;
        r.total = count;
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //更新报表信息
    Result update(JObject jObject)
    {
        Result r = new Result();
        string sql = @" 
UPDATE mes_reportpage t SET t.pagename='{0}',t.pageurl='{1}',t.pagetype='{2}',t.factoryid='{3}',t.workcenterid='{4}',t.modifyemployeeid = '{5}',t.isnew = '{6}',t.modifydate = sysdate 
WHERE t.pageid='{7}' ";

        string pageid = jObject["pageid"].ToString();
        string pagename = jObject["pagename"].ToString();
        string pageurl = jObject["pageurl"].ToString();
        string pagetype = jObject["pagetype"].ToString();
        string workcenterid = jObject["workcenterid"] != null ? jObject["workcenterid"].ToString() : "";
        string factoryid = jObject["factoryid"] != null ? jObject["factoryid"].ToString() : "";
        string isnew = "0";
        if (jObject["isnew"] != null && jObject["isnew"].ToString() == "1")
        {
            isnew = "1";
        }
        string employeeid = "";
        if (jObject["employeeid"] != null)
        {
            employeeid = jObject["employeeid"].ToString();
        }

        sql = string.Format(sql, pagename, pageurl, pagetype, factoryid, workcenterid, employeeid, isnew, pageid);

        int count = dal.ExecuteSql(sql);

        r.data = null;
        r.total = count;
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //更新启用状态
    Result updateStatus(JObject jObject)
    {
        Result r = new Result();
        string sql = @" UPDATE mes_reportpage t SET t.status='{1}',t.modifyemployeeid = '{2}',t.modifydate = sysdate WHERE t.pageid='{0}' ";

        string status = "";
        if (jObject["status"] != null)
        {
            status = jObject["status"].ToString() == "True" ? "1" : "0";
        }

        string employeeid = "";
        if (jObject["employeeid"] != null)
        {
            employeeid = jObject["employeeid"].ToString();
        }

        sql = string.Format(sql, jObject["pageid"].ToString(), status, employeeid);

        int count = dal.ExecuteSql(sql);

        r.data = null;
        r.total = count;
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //删除报表
    Result deletePage(JObject jObject)
    {
        string sql = string.Format("delete mes_reportpage t where t.pageid = '{0}'", jObject["pageid"].ToString());
        int count = dal.ExecuteSql(sql);
        //删除所有人员配置该报表的记录
        dal.ExecuteSql(string.Format("delete mes_employeereportpage t where t.pageid = '{0}'", jObject["pageid"].ToString()));

        Result r = new Result();
        r.data = null;
        r.total = count;
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //查询分厂及工区信息
    Result getWorkcenter()
    {
        string sql = @"
SELECT f.factoryid,f.factoryname,w.workcenterid,w.workcentername
FROM factory f 
LEFT JOIN Workcenter w ON w.factoryid = f.factoryid
WHERE w.factoryid IS NOT NULL AND w.notes is null 
ORDER BY f.factoryid";
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dt.Rows.Count);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }
}